﻿import QtQuick 2.12
import QtQuick 2.9
import QtQuick.Controls 2.2
import QtQuick.Window 2.12
import TaoQuick 1.0
import Top 1.0
import Qt.labs.platform 1.1

Window {
    id:comuWindowId
    width: 640
    height: 480
    visible: true
    title: qsTr("聊天窗口")
    property NewTcpClient client
    //用户名
    property string userAccount: ""
    onVisibleChanged: {
        if(visible) {
            client = GlobalManager.tcpManager.getClientPtr(userAccount)
            console.log("ptr client = " + client +"acount = " + userAccount)
        }
    }
    MoviePlayCom {
        id: moviePlayerId
        visible: false
    }

    Item {
        anchors.fill: parent
        Column {
            anchors.horizontalCenter: parent.horizontalCenter
            CusLabel {
                text:"消息框："
                font.bold: true
                font.pixelSize: 18
            }

            ScrollView {
                id: displayInfoId
                width: 580
                height: 250
                anchors.horizontalCenter: parent.horizontalCenter
                clip: true
                TextArea {
                    verticalAlignment: Text.AlignTop
                    horizontalAlignment: Text.AlignLeft
                    leftPadding: 10
                    topPadding: 10
                    color: "black"
                    width: parent.width
                    height: parent.height
                    clip: true
                    font.pixelSize: 16
                    readOnly: true
                    selectByMouse: true
                    textFormat: Text.RichText
                    selectedTextColor: "#1fBFC0"
                    text: client.transferProxy.comInfo
                    wrapMode: TextEdit.WrapAnywhere
                    cursorPosition: text.length
                    background: Rectangle {
                        color: "#3069b7eb"
                        radius: 10
                        border.width: 2
                        border.color: "white"
                    }
                    onLinkActivated: {
                        console.log("you chose link" + link)
                        moviePlayerId.moviePath = link
                        moviePlayerId.visible = true
                    }
                }
            }
            Row {
                spacing: 50
                CusLabel {
                    text:"输入框："
                    font.bold: true
                    font.pixelSize: 20
                }
                CusButton_ImageBackground {
                    height: 40
                    width: 40
                    btnImgUr: "qrc:/Image/openFile.png"
                    anchors.bottom: parent.bottom
                    onClicked: {
                        fileDialogId.open()
                    }
                    FileDialog {
                        id: fileDialogId
                        onAccepted: {
                            console.log("path = " + fileDialogId.currentFile)
                            //GlobalManager.tcpClientManager.sendImage(fileDialogId.currentFile)
                            client.sendImage(fileDialogId.currentFile)
                        }
                    }
                }
                CusButton_ImageBackground {
                    height: 40
                    width: 40
                    btnImgUr: "qrc:/Image/fries.png"
                    anchors.bottom: parent.bottom
                    onClicked: {
                        fileDialogId1.open()
                    }
                    FileDialog {
                        id: fileDialogId1
                        onAccepted: {
                            console.log("path = " + fileDialogId1.currentFile)
                            client.transferProxy.sendMovie(fileDialogId1.currentFile)
                        }
                    }
                }
                CusButton_ImageBackground {
                    height: 40
                    width: 40
                    btnImgUr: "qrc:/Image/clrear.png"
                    anchors.bottom: parent.bottom
                    onClicked: {
                        client.transferProxy.comInfo=""
                        client.transferProxy.removeInfoDir();
                    }
                }
            }
            ScrollView {
                width: 580
                height: 100
                y:20
                anchors.horizontalCenter: parent.horizontalCenter
                TextArea {
                    id: sendMessageInfoId
                    verticalAlignment: Text.AlignTop
                    horizontalAlignment: Text.AlignLeft
                    width: parent.width
                    height: parent.height
                    leftPadding: 10
                    topPadding: 10
                    color: "black"
                    font.pixelSize: 16
                    selectByMouse: true
                    selectedTextColor: "#1fBFC0"
                    wrapMode: TextEdit.WrapAnywhere
                    cursorPosition: text.length
                    background: Rectangle {
                        color: "transparent"
                        border.width: 1
                        radius: 5
                        border.color: "black"
                    }
                }
            }
            Item {
                height: 50
                width: 580
                CusButton {
                    anchors.left: parent.left
                    anchors.bottom: parent.bottom
                    text: "分手"
                    height: 35
                    width: 100
                    onClicked: {
                        client.disconnectServer();
                        //comInfo消息为空
                        client.comInfo = ""
                        //UserState 0表示没有连接，1表示已连接
                        ClientDataRecordSqlTableModel.UserState=0
                        comuWindowId.close()
                    }
                }
                CusButton {
                    anchors.right: parent.right
                    anchors.bottom: parent.bottom
                    text: "发送"
                    height: 35
                    width: 100
                    onClicked: {
                        client.transferProxy.sendTextInfo(sendMessageInfoId.text)
                        sendMessageInfoId.text = ""
                    }
                }
            }
        }
    }
}
